﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Administration_layout_index : System.Web.UI.Page
{
    private static XmlDocument xmlDoc = new XmlDocument();
    private static XmlNodeList nodeListFont;
    private static XmlNodeList nodeListBack;
    private static String path = "/CMSProject/Data/design.xml";
    private static String[,] fonts;
    private static String[,] backgrounds;
    private static Object[] listFont;
    private static Object[] listBack;

    void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            xmlDoc.Load(Server.MapPath(path));

            
            fonts = getDataFromXML(true);
            backgrounds = getDataFromXML(false);

            listFont = new Object[] {
                HeadFontSize, HeadFontType, HeadTextColorBox, HeadBackColorBox, 
                ContentFontSize, ContentFontType, ContentTextColorBox, ContentBackColorBox,
                LinkFontSize, LinkFontType, LinkTextColorBox, LinkBackColorBox,
                TableFontSize, TableFontType, TableBorderColorBox, TableBackColorBox,
                ListFontSize, ListFontType, ListTextColorBox, ListBackColorBox
            };

            listBack = new Object[] {
                HeaderColorBox, FooterColorBox, Row1ColorBox, Row2ColorBox, Row3ColorBox
            };

            setDataToForm(fonts,backgrounds);
            /*HeadFontSize.SelectedValue = fonts[0][0];
            HeadFontType.SelectedValue = fonts[0][1];
            HeadTextColorBox.Text = fonts[0][2];
            HeadBackColorBox.Text = fonts[0][3];

            ContentFontSize.SelectedValue = fonts[1][0];
            ContentFontType.SelectedValue = fonts[1][1];
            ContentTextColorBox.Text = fonts[1][2];
            ContentBackColorBox.Text = fonts[1][3];

            LinkFontSize.SelectedValue = fonts[2][0];
            LinkFontType.SelectedValue = fonts[2][1];
            LinkTextColorBox.Text = fonts[2][2];
            LinkBackColorBox.Text = fonts[2][3];

            TableFontSize.SelectedValue = fonts[3][0];
            TableFontType.SelectedValue = fonts[3][1];
            TableBorderColorBox.Text = fonts[3][2];
            TableBackColorBox.Text = fonts[3][3];

            ListFontSize.SelectedValue = fonts[4][0];
            ListFontType.SelectedValue = fonts[4][1];
            ListTextColorBox.Text = fonts[4][2];
            ListBackColorBox.Text = fonts[4][3];            */
        }
    }

    private void setDataToForm(String[,] valuesFont, String[,] valuesBack)
    {
        int one = valuesFont.Length;
        int two = valuesFont.GetLength(1);
        int first = 0;
        int second = 0;

        for (int i = 0; i < listFont.Length; i++)
        {           
            if (listFont[i] is DropDownList)
            {
                ((DropDownList)listFont[i]).SelectedValue = valuesFont[first, second];
            }
            else
            {
                ((TextBox)listFont[i]).Text = valuesFont[first, second];
            }
            if (second == 3)
            {
                first++;
                second = 0;
            }
            else
            {
                second++;
            }
        }

        one = valuesBack.Length;
        two = valuesBack.GetLength(1);
        for (int i = 0; i < valuesBack.Length; i++)
        {
            ((TextBox)listBack[i]).Text = valuesBack[i, 0];
        }
    }

    private String[,] getDataFromForm(int one, int two)
    {
        String[,] values = new String[one, two];
        int first = 0;
        int second = 0;

        for (int i = 0; i < listFont.Length; i++)
        {
            if (listFont[i] is DropDownList)
            {
                values[first, second] = ((DropDownList)listFont[i]).SelectedItem.Value.ToString();

            }
            else
            {
                values[first, second] = ((TextBox)listFont[i]).Text.ToString();
            }

            // ugly but works ...
            if (second == 3)
            {
                first++;
                second = 0;
            }
            else
            {
                second++;
            }
        }
        return values;
    }

    private String[,] getDataFromBackForm(int one, int two)
    {
        String[,] values = new String[one, two];

        for (int i = 0; i < listBack.Length; i++)
        {
            values[i, 0] = ((TextBox)listBack[i]).Text.ToString();
        }
        return values;
    }

    private String[,] getDataFromXML(bool font)
    {
        if (font)
        {
            nodeListFont = xmlDoc.SelectNodes("/Design/Font");
            int first = nodeListFont[0].ChildNodes.Count;
            int second = nodeListFont[0].ChildNodes[0].ChildNodes.Count;

            String[,] tmp = new String[first, second];
            for (int i = 0; i < first; i++)
            {
                for (int j = 0; j < second; j++)
                {
                    tmp[i, j] = nodeListFont[0].ChildNodes[i].ChildNodes[j].InnerText.ToString();
                }
            }
            return tmp;
        }
        else
        {
            nodeListBack = xmlDoc.SelectNodes("/Design/Background");
            int first = nodeListBack[0].ChildNodes.Count;
            int second = nodeListBack[0].ChildNodes[0].ChildNodes.Count;

            String[,] tmp = new String[first, second];
            for (int i = 0; i < first; i++)
            {
                for (int j = 0; j < second; j++)
                {
                    tmp[i, j] = nodeListBack[0].ChildNodes[i].ChildNodes[j].InnerText.ToString();
                }
            }
            return tmp;
        }
    }

    private void setDataToXML(bool font, String[,] toSave)
    {
        if (font)
        {
            int one = toSave.GetLength(0);
            int two = toSave.GetLength(1);
            nodeListFont = xmlDoc.SelectNodes("/Design/Font");
            for (int i = 0; i < one; i++)
            {
                for (int j = 0; j < two; j++)
                {
                    nodeListFont[0].ChildNodes[i].ChildNodes[j].InnerXml = toSave[i, j];
                }
            }
        }
        else
        {
            int one = toSave.GetLength(0);
            int two = toSave.GetLength(1);
            nodeListBack = xmlDoc.SelectNodes("/Design/Background");
            for (int i = 0; i < one; i++)
            {
                nodeListBack[0].ChildNodes[i].ChildNodes[0].InnerXml = toSave[i, 0];
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        xmlDoc.Load(Server.MapPath(path));

        listFont = new Object[] {
                HeadFontSize, HeadFontType, HeadTextColorBox, HeadBackColorBox, 
                ContentFontSize, ContentFontType, ContentTextColorBox, ContentBackColorBox,
                LinkFontSize, LinkFontType, LinkTextColorBox, LinkBackColorBox,
                TableFontSize, TableFontType, TableBorderColorBox, TableBackColorBox,
                ListFontSize, ListFontType, ListTextColorBox, ListBackColorBox
            };
        listBack = new Object[] {
                HeaderColorBox, FooterColorBox, Row1ColorBox, Row2ColorBox, Row3ColorBox
            };
        setDataToXML(true, getDataFromForm(5, 4));
        xmlDoc.Save(Server.MapPath(path));
        setDataToXML(false, getDataFromBackForm(5, 1));
        xmlDoc.Save(Server.MapPath(path));
    }
}